<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/assets/cashier/css/main.css"/>
    <link rel="stylesheet" href="/assets/cashier/css/iconfont.css">
    <link rel="stylesheet" href="/assets/cashier/css/print.css">
    <script type="text/javascript" src="/assets/cashier/js/jquery-1.11.3.js"></script>
    <script type="text/javascript" src="/assets/cashier/js/main.js"></script>
    <script type="text/javascript" src="/assets/cashier/js/jquery.jqprint-0.3.js"></script>
    <script type="text/javascript" src="/assets/cashier/js/jquery-migrate-1.2.1.min.js"></script>
    <!--<script type="text/javascript" src="/assets/cashier/js/lang_jyp.js"></script>-->
    <title>退换货</title>
    <style>
        input::-webkit-input-placeholder { color:#ccc; }
        input::-moz-placeholder { color:#ccc; } /* firefox 19+ */
        input::-ms-input-placeholder { color:#ccc; } /* ie */
        .change-search .change-search-head .change-input{color: #333}
        .table{padding: 0px}
        .table .table-list{border-color: #dedede; padding: 0px;}
         .change-box{
            overflow-x: hidden;
            overflow-y: auto;
        }
        .order-detail-body{
            overflow-x: hidden;
            overflow-y: auto;
        }
        .order-table-list .list-item{
        	text-align: center;
        }
        .list-item .num{
        	font-size: 14px;
        	overflow: hidden;
		    white-space: nowrap;
		    text-overflow: ellipsis;
        }
        .menu-list-num{
        	width: 40px;
        	height: 25px;
        	line-height: 25px;
        	border: 1px solid #D1D1D1;
        }
        .refund-input{
        	width: 50px;
        	height: 25px;
        	border: 1px solid #D1D1D1;
        	text-align: center;
        }
        #reason span.active{
        	color: #FF6600;
        }
        .order-table-list .list-item .reason-item{
        	width: 90%;
        	overflow: hidden;
		    white-space: nowrap;
		    text-overflow: ellipsis;
		    color: #f60;
        }
         .order-table-list .money{
         	font-size: 14px;
         }
    </style>
</head>
<body>
    <div class="container">
        <div class="nav">
            <div class="user-head">
                <img src="/assets/logo.png"/>
            </div>
            <div class="nav-con">
                <ul>
                    <li class="nav-list">
                        <a href="/cashier/index/index/index">
                            <span class="iconfont icon-shouyin"></span>
                            <p data-locale="msg_shouyin">收银台</p>
                        </a>
                    </li>
                    <li class="nav-list">
                        <a href="/cashier/index/index/order">
                            <span class="iconfont icon-guadan"></span>
                            <p data-locale="msg_guadan">挂单</p>
                        </a>
                    </li>
                    <li class="nav-list">
                        <a href="/cashier/index/index/members">
	                        <span class="iconfont icon-huiyuan"></span>
	                        <p data-locale="msg_huiyuan">会员</p>
                        </a>
                    </li>
                    <li class="nav-list">
                        <a href="/cashier/index/index/count">
                            <span class="iconfont icon-tongji"></span>
                            <p data-local="msg_tongji">统计</p>
                        </a>
                    </li>
                    <li class="nav-list active">
                        <a href="/cashier/index/index/change">
                            <span class="iconfont icon-tuihuo"></span>
                            <p data-locale="msg_tuihuanhuo">退换货</p>
                        </a>
                    </li>
                </ul>
            </div>
            <div class="nav-foot">
                <div class="nav-foot-list nav-lock">
                    <span class="iconfont icon-suo"></span>
                    <p data-locale="suoding">锁定</p>
                </div>
                <div class="nav-foot-list nav-cancel">
                    <a href="/cashier/index/index/exchange">
                        <span class="iconfont icon-tuichu"></span>
                        <p data-locale="msg_jiaoban">交班</p>
                    </a>
                </div>
            </div>
        </div>
        <div class="content" style="padding-right: 25px">
            <div class="change-search">
                <div class="change-search-head">
                    <div style="width: 575px;height: 40px;margin: 0 auto">
                        <input type="text" name="pattern" class="change-input pull-left" placeholder="扫描条形码 / 输入单号 /输入会员手机号">
                        <div class="btn pull-left search" style="background: #51d4a8;color: #fff;border-radius: 0;height: 40px;line-height: 40px">搜索</div>
                    </div>
                </div>
                <div class="table" style="margin-top: 20px;border: 1px solid #dedede ">
                    <table class="table-list " id="order-table" rules="rows">
                        <thead>
                        <tr >
                            <td class="text-center" data-locale="msg_orderId">订单号</td>
                            <td class="text-center" data-locale="msg_date">日期</td>
                            <td class="text-center" data-locale="msg_huiyuan">会员</td>
                            <td class="text-center" data-locale="msg_zje">总金额</td>
                            <td class="text-center" data-locale="msg_yhje">优惠金额</td>
                            <td class="text-center" data-locale="msg_yunfei">运费</td>
                            <td class="text-center" data-locale="msg_ssje">实收金额</td>
                            <td class="text-center" data-locale="msg_paytype">支付方式</td>
                        </tr>
                        </thead>
                        <tbody class="data-body"></tbody>
                        <tfoot><tr style="height: 70px;"><td colspan="8" class="text-center"></td></tr></tfoot>
                    </table>
                </div>
            </div>
            <div class="change-detail hide">
                <div class="order-detail">
                    <div class="order-detail-head">
                        <div style="font-size: 18px;line-height: 30px"><span data-locale="msg_orderId">单号</span>：<span class="order-id"></span></div>
                        <div style="font-size: 14px;line-height: 30px"><span data-locale="msg_time">时间</span>：<span class="order-time"></span><span style="margin-left: 20px"><span>收银员</span>：<span class="order-waiter">-</span></span></div>
                    </div>
                    <div class="order-detail-body">
                       
                    </div>
                    <div class="order-total">
                        <div class="order-total-num">
                            <span><span data-locale="msg_num">数量</span>：<span class="totalNum"></span></span>
                            <span><span data-locale="msg_total">合计</span>：￡<span class="totalPrice"></span></span>
                            <span><span data-locale="msg_youhui">优惠</span>：￡<span class="discountPrice"></span></span>
                        </div>
                        <div class="order-total-price">
                            <span class="pull-right"><span data-locale="msg_shifu">实付</span>：<span style="font-size: 20px;color: #f11f03;">￡<span class="deal"></span></span></span>
                            <span class="pull-right" style="font-size:14px; margin-right: 20px;"><span data-locale="msg_yunfei">运费</span>：￡<span class="shippingPrice"></span></span>
                        </div>
                        <div class="order-detail-list-pay"></div>
                    </div>
                </div>
                <div class="order-table">
                    <div class="table-con">
                        <div class="order-table-list" style="color: #999;">
                            <div class=" list-item" data-locale="msg_shop">商品</div>
                            <div class=" list-item" data-locale="msg_changenum">退换数量</div>
                            <div class=" list-item" data-locale="msg_changetype">退换类型</div>
                            <div class="list-item" data-locale="msg_tkje">退款金额</div>
                            <div class="list-item" data-locale="msg_reason">原因</div>
                            <div class=" list-item" data-locale="msg_isDamaged">是否货损</div>
                        </div>
                        <div class="change-box">
                        	<ul>
                        		
                        	</ul>
                        </div>
                       
                    </div>
                    <div class="order-foot">
                        <div class="pull-right">
                            <div class="pull-left"><span data-locale="msg_needRefund">需退款</span>：<span class="money">￡<span class="return">0</span></span></div>
                            <div class="btn btn-active pull-left cancelButton" style="margin: 17px 20px;background: #d1d1d1" data-locale="msg_quxiao">取消</div>
                            <div class="btn btn-active pull-left returnButton" style="margin-top: 17px" data-locale="msg_queren">确认</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="popbg">
        <div class="popbox" id="tuikuan" style="height: 320px">
            <div class="pop-head" data-locale="msg_refundSet">请设置退款金额和退款方式</div>
            <div class="pop-body">
                <div class="pop-input-box">
                    <div class="pull-left">￡<input type="text" name="discount" class="discount-input discount-val" style="width: 300px;font-size: 30px;color: #f60;" readonly="readonly"></div>
                    <div class="pull-right">
                        <span style="color: #d1d1d1;margin-right: 20px;cursor:pointer;" class="refund-cash" data-locale="msg_cash">现金</span>
                        <span style="color: #51d4a8;cursor: pointer" class="refund-balance" data-locale="msg_yue">余额</span>
                    </div>
                </div>
                <div class="pop-total" style="margin-top: 0">
                    <span ><span data-locale="msg_shopzje">商品总金额</span>：￡<span class="oldPrice">123</span></span>
                    <span style="margin:0 30px"><span data-locale="msg_bdzk">本单折扣</span>：<span class="discount">8.5</span></span>
                    <span ><span data-locale="msg_sfje">实付金额</span>：￡<span class="newPrice">100</span></span>
                </div>
                <div class="pop-input-box" style="margin-top: 0">
                    <input type="text" name="note" class="discount-input note-val" placeholder="请填写备注信息">
                </div>
            </div>
            <div class="pop-foot">
                <div class="leftButton pull-left refund-cancel" data-locale="msg_quxiao">取消</div>
                <div class="rightButton pull-left refund-submit" data-locale="msg_queren">确认</div>
            </div>
        </div>
        <div class="popbox" id="reason">
            <div class="pop-head" data-locale="msg_refundReason">请填写你或选择退货原因</div>
            <div class="pop-body" style="margin-top: 10px">
                <div class="pop-input-box" style="margin-top: 0">
                    <input type="text" name="note" class="discount-input reason-val">
                </div>
                <div style="margin-top: 20px">
                    <span class="reason-list active" data-id="0" data-locale="msg_shopDamaged">货物破损</span>
                    <span class="reason-list" data-id="1" data-locale="msg_shopOvertime">商品过期</span>
                    <span class="reason-list" data-id="2" data-locale="msg_userReason">用户原因</span>
                    <span class="reason-list" data-id="3" data-locale="msg_countError">结算错误</span>
                    <span class="reason-list" data-id="4" data-locale="msg_getErrorshop">拿错商品</span>
                    <span class="reason-list" data-id="5" data-locale="msg_otherReason">其他原因</span>   
                </div>
            </div>
            <div class="pop-foot">
                <div class="centerButton reason-submit-btn" data-locale="msg_quren">确认</div>
            </div>
        </div>
        <!--锁定-->
         <div class="popbox" id="lock">
            <div class="pop-head" data-locale="msg_qxsd">请填写你的密码取消锁定</div>
            <div class="pop-body">
                <input class="lock-input" name="password">
            </div>
            <div class="pop-foot">
                <div class="centerButton lock-btn" data-locale="msg_queren">确认</div>
            </div>
        </div>
        <div id="printer" class="print-refund">
			<div class="code"><img src="../../../assets/logo.png"/></div>
			<div class="title">日出行中国超市</div>
			<div class="en-title">Sunrise Chinese Market Ltd</div>
			<div class="address">
				<p>84 Colman Road, Norwich, NR4 7EH</p>		
				<p>01603 927952	</p>	
			</div>
			<div class="line-type"><span class="pull-left offline">OFFLINE</span><span class="pull-right">顾客线下退货小票</span></div>
			<div class="print-list">收银员工：<span class="cashier-name"></span></div>
			<div class="print-list">退货时间：<span class="refund-time"></span></div>
			<div class="print-list">线下退货：<span class="refund-id"></span></div>
			<div class="print-list">退货备注：<span class="refund-remark"></span></div>
			<div class="print-list vip-name-box">会员姓名：<span class="vip-name"></span></div>
			<div class="print-list">微信号码：<span class="vip-weixin"></span></div>
			<div class="print-list">送货地址：<span class="vip-address"></span></div>
			<div class="print-list">送货邮编：<span class="vip-site"></span></div>
			<div class="print-list">联系电话：<span class="vip-phone"></span></div>
			<div class="print-list">退款方式：<span class="print-refund-payMode"></span></div>
			<div class="line"></div>
			<div class="print-table-head">
				<span>单价(￡)</span>
				<span>数量</span>
				<span>是否货损</span>
				<span>金额(￡)</span>
			</div>
			<div class="line"></div>
			<div class="print-table-body"></div>
			<div class="line"></div>
			<div class="print-table-head">
				<span>共计</span>
				<span class="print-table-total-food"></span>
				<span class="print-table-total-price"></span>
			</div>
			<div class="line"></div>
			<div class="consumption"></div>
			
			<div class="line"></div>
			<div class="print-list text-center">VAT RECEIPT SUMMARY</div>
			<div class="print-list text-center">NET：<span class="vat-net"></span></div>
			<div class="print-list text-center">20%VAT：<span class="vat-20"></span></div>
			<div class="print-list text-center">GROSS：<span class="vat-gross"></span></div>
			<div class="print-list text-center">VAT No. : 157965263</div>
			<div class="line"></div>
			<div class="print-time">打印时间：<span class="print-time-detail"></span></div>
			<div class="print-time">感谢惠顾，期待您的再次光临</div>
			<div class="print-time">加入会员，更多折扣，更多优惠，更多方便</div>
			<div class="print-time">扫我↓</div>
			<div class="code"><img src="../../../assets/cashier/img/richubao.jpg" /></div>
		</div>
		<div id="online" class="print-online">
        	<div class="code"><img src="../../../assets/logo.png"/></div>
			<div class="title">日出行中国超市</div>
			<div class="en-title">Sunrise Chinese Market Ltd</div>
			<div class="address">
				<p>84 Colman Road, Norwich, NR4 7EH</p>		
				<p>01603 927952	</p>	
			</div>
			
			<div class="line-type"><span class="pull-left">ON-LINE</span><span class="pull-right">顾客线上消费送货小票</span></div>
			<div class="print-list">送货时间：<span class="delivery-time"></span></div>
			<div class="print-list">收银员工：<span class="cashier-name"></span></div>
			<div class="print-list">消费时间：<span class="order-time"></span></div>
			<div class="print-list">线上订单：<span class="order_id"></span></div>
			
			<div class="print-list vip-name-box">会员姓名：<span class="vip-name"></span></div>
			<div class="print-list">微信号码：<span class="vip-weixin"></span></div>
			<div class="print-list">送货地址：<span class="vip-address"></span></div>
			<div class="print-list">送货邮编：<span class="vip-site"></span></div>
			<div class="print-list">联系电话：<span class="vip-phone"></span></div>
			<div class="print-list">备注：<span class="vip-remark"></span></div>
			<div class="print-list">支付方式：<span class="print-order-payMode"></span></div>
			<div class="line"></div>
			<div class="print-table-head">
				<span>单价(￡)</span>
				<span>数量</span>
				<span>金额(￡)</span>
			</div>
			<div class="line"></div>
			<div class="print-table-body"> </div>
			<div class="line"></div>
			<div class="print-table-head">
				<span>共计</span>
				<span class="print-table-total-food"></span>
				<span class="print-table-total-price"></span>
			</div>
			<div class="line"></div>
			<div class="consumption"></div>
			
			<div class="line"></div>
			<div class="print-list text-center">VAT RECEIPT SUMMARY</div>
			<div class="print-list text-center">NET：<span class="vat-net"></span></div>
			<div class="print-list text-center">20%VAT：<span class="vat-20"></span></div>
			<div class="print-list text-center">GROSS：<span class="vat-gross"></span></div>
			<div class="print-list text-center">VAT No. : 157965263</div>
			<div class="line"></div>
			<div class="print-time">打印时间：<span class="print-time-detail"></span></div>
			<div class="print-time">感谢惠顾，期待您的再次光临</div>
			<div class="print-time">加入会员，更多折扣，更多优惠，更多方便</div>
			<div class="print-time">扫我↓</div>
			<div class="code"> <img src="../../../assets/cashier/img/richubao.jpg" /></div>
		</div>
    </div>
     <div class="tip hide"></div>
    <script type="text/javascript">
    $(function (){
        lock();
        var windowWidth=document.body.clientWidth;
        var windowHeight=$('.nav').height();
        var boxHeight=windowHeight-180+'px';
        var detailBodyHeight=windowHeight-245+'px';
        var tableWidth=windowWidth-597+"px";
        $('.change-box').css({height:boxHeight});
        $('.order-detail-body').css({height:detailBodyHeight})
        $('.order-table').css({width:tableWidth});
        
        $('.chage-input').attr('placeholder',lang('CHANGE_SEARCH_PLACE'))
        $('.note-val').attr('placeholder',lang('INDEX_NOTE_PLACE'))
        
        var units=SITE_CONFIG.units;
        //搜索
        $('.change-input')[0].focus();
        $('.change-input').keydown(function (event) {
            if(event.keyCode == 13){
              search()
            }
        })
            
        $('.search').click(function () {
           search();
        });
        
        var order_page=1,order_total_page=1;
       search();
        $('body').on('click','#order-table .page-next',function(){
            if (parseInt(order_page)+1 <= order_total_page) {
               order_page++;
               search();
            }
        });
         $('body').on('click','#order-table .page-prev',function(){
            if (parseInt(order_page)-1 > 0) {
                order_page -= 1;
                search();
            }
        });
        $('body').on('click','#order-table .page-target',function(){
            order_page = parseInt($(this).html());
            search();
        });
        function search(){
        	var params={page:order_page};
        	var pattern=$('.change-input').val();
            if(pattern){
              params.keyword=pattern;
              params.page=1;
            }
            console.log(params)
            $.ajax({
                type:'GET',
                data:params,
                url:urlHead+'//order/get_orders',
                dataType:'json',
                success:function (json) {
                    if(json.total  == 0){
                        tip(lang('CHANGE_ORDER_NOT_FIND'));
                        return;
                    }
                    console.log(json.data)
                    var total=json.last_page;
                    var page=json.current_page;
	                order_total_page = json.last_page;
	                
                    var html = '';
                    for(var i =0;i<json.data.length;i++){
                    	
                        var order = json.data[i];
                        var user = '-';
                        var actualMoney=parseFloat(order.total-order.cut_fee)+parseFloat(order.shipping_fee);
                       	
                        if(order.nickname) user = order.nickname;
                        if(order.first_name && order.last_name) user = order.first_name+' '+order.last_name;
                        var paymode = SITE_CONFIG.pay_types[0][SITE_LANG];
                        if(order.money == 0) paymode = '';
                        if(order.pay_mode>0) paymode+= SITE_CONFIG.pay_types[order.pay_mode][SITE_LANG]; 
                        html += '<tr data-id="'+order.id+'">'+
                                    '<td class="text-center">'+order.order_id+'</td>'+
                                    '<td class="text-center">'+order.create_time+'</td>'+
                                    '<td class="text-center">'+user+'</td>'+
                                    '<td class="text-center">'+order.total+'</td>'+
                                    '<td class="text-center">'+order.cut_fee+'</td>'+
                                    '<td class="text-center">'+order.shipping_fee+'</td>'+
                                    '<td class="text-center">'+(actualMoney).toFixed(2)+'</td>'+
                                    '<td class="text-center">'+paymode+'</td>'+
                                '</tr>';
                    }
                    $('.data-body').html(html);
                    if(parseInt(total)>1){
                    	var pageHtml=paginate(total,page);
                        $('#order-table tfoot tr td').html(pageHtml);
                    }else{
                    	$('#order-table tfoot').html('');
                    }
                }
            })
        }
        
        var items,discount,user_id,total,pay_fee,order_id,cut_fee,totalNum,user,refundOrder;
        $('body').on('click','.data-body tr',function(){
            var id=$(this).attr('data-id');
            $.ajax({
                type:'GET',
                url:urlHead+'//order/get_order/'+id,
                dataType:'json',
            })
            .done(function(json) {
            	console.log(json)
            	refundOrder=json.order;
            	user=json.user
            	items=json.items;
            	user_id=json.order.user_id;
            	discount=json.order.discount;
            	total=json.order.total;
            	pay_fee=json.order.pay_fee;
            	cut_fee=json.order.cut_fee;
            	order_id=json.order.id;
            	var refunds=json.refunds;
            	
            	console.log(refunds)
            	if(discount == 0) discount=1;
                $('.change-search').hide();
                $('.order-id').html(json.order.order_id);
                $('.order-time').html(json.order.create_time);
                if(json.cashier) $('.order-waiter').html(json.cashier.username);
                $('.totalPrice').html(json.order.total);
                $('.discountPrice').html(json.order.cut_fee);
                $('.shippingPrice').html(json.order.shipping_fee);
                var deal = json.order.total - json.order.cut_fee + parseInt(json.order.shipping_fee);
                $('.deal').html((deal).toFixed(2));
                var html = '';
               totalNum = 0;
                for(var i=0;i<json.items.length;i++){
                	
                    var item = json.items[i];
                    var unit='';
	               for(var j in units){
	                    if(item.product.unit == units[j].id){
	                        if(SITE_LANG=='chinese'){
	                    		 unit='/'+units[j].title;   
	                    	}else{
	                    		 unit='/'+units[j].en_title;   
	                    	}
	                    }
	                }
                    totalNum += parseFloat(item.qty);
                    html += '<div class="order-detail-list">'+
                                '<div class="num" style="font-size:14px">'+(SITE_LANG=='chinese'?item.product.title:item.product.en_title)+'('+(SITE_LANG=='chinese'?item.item.title:item.item.en_title)+')</div>'+
                                '<div>'+
                                    '<span class="money" style="font-size:12px">￡<span class="price">'+item.item.price+unit+'</span></span>'+
                                    '<span class="num" style="margin-left: 20px;font-size:12px">x '+item.qty+'</span>'+
                                '</div>';
                               ;
                    if(refunds.length>0){
                    	$.each(refunds,function(index,list){
                    		if(item.id == list.order_item_id){
                    			console.log(item.id)
                    			if(list.status == 3){
                    				html+= '<div class="btn btn-default list-select" data-id="'+i+'">'+lang('CHANGE_CHOSE')+'</div>'
                            			+'</div>';
                            			refunds.splice(index,1)
                            			return false;
                    			}else{
                    				html+= '<div class="btn btn-primary list-select" data-id="'+i+'">'+lang('CHANGE_CHOSE')+'</div>'
                            			+'</div>';
                            			refunds.splice(index,1)
                            			return false;
                    			}	
                    		}else{
                    			html+= '<div class="btn btn-primary list-select" data-id="'+i+'">'+lang('CHANGE_CHOSE')+'</div>'
                            		+'</div>';
                            		refunds.splice(index,1)
                            		return false;
                    		}	
                    	})
                    }else{
                    	html+= '<div class="btn btn-primary list-select" data-id="'+i+'">'+lang('CHANGE_CHOSE')+'</div>'
                            +'</div>';
                    }
                }
                $('.totalNum').html(totalNum);
                $('.order-detail-body').html(html);
				
                if(json.order.pay_fee>0){
                    $('.order-detail-list-pay').append('<span class="pull-right">'+SITE_CONFIG.pay_types[json.order.pay_mode][SITE_LANG]+'：<span class="money">￡<span>'+json.order.pay_fee+'</span></span></span>');
                }
                if(json.order.money>0){
                    $('.order-detail-list-pay').append('<span class="pull-right">'+SITE_CONFIG.pay_types[0][SITE_LANG]+'：<span class="money">￡<span>'+json.order.money+'</span></span></span>');
                }

                $('.change-detail').show();
            })
            .fail(function(xhr) {
               if(xhr.responseJSON.error == 404){
                    tip(lang('CHANGE_ORDER_NOT_FIND'));
               }
            })
        })
       
        //退货详情页
        $('body').on('click','.order-detail-list .list-select',function(){
        	if($(this).hasClass('btn-default')) return;
            $(this).removeClass('btn-primary').addClass('btn-default');
            var shopId=$(this).attr('data-id');
            var item=items[shopId];
            var refundMoney=(item.qty*item.item.price*discount).toFixed(2);
            
			items[shopId].exchange={
				type:0,
				money:refundMoney,
				num:item.qty
			}
			console.log(items);
            var unit='';
           for(var j in units){
                if(items[shopId].product.unit == units[j].id){
                    if(SITE_LANG=='chinese'){
                		 unit='/'+units[j].title;   
                	}else{
                		 unit='/'+units[j].en_title;   
                	}
                }
            }
            var li=''
            li+='<li class="order-table-list" data-index="'+shopId+'">'
                +'<div style="line-height: 30px;padding-top: 13px;" class="list-item" data-id="'+item.id+'">'
                +    '<p class="num">'+(SITE_LANG=='chinese'?item.product.title:item.product.en_title)+'</p>'
                +    '<span class="money" style="float:left">￡<span class="price">'+item.item.price+unit+'</span></span>'
                +'</div>'
                +'<div class="list-item">'
                +    '<span class="iconfont icon-jian"></span>'
                +    '<input type="text" class="menu-list-num" value="'+item.qty+'"/>'
                +    '<span class="iconfont icon-add"></span>'
                +'</div>'
                +'<div class="list-item" style="padding-top: 25px">'
                +    '<div class="switch-tab" style="margin:0 auto">'
                +       ' <span class="tab tab1 active" data-type="0">'+lang('CHANGE_SHOP_TUI')+'</span>'
                +        '<span class="tab tab2" data-type="1">'+lang('CHANGE_SHOP_HUAN')+'</span>'
                +    '</div>'
                +'</div>'
                +'<div class="list-item">'
                +		'<input type="text" class="refund-input" value="'+item.exchange.money+'"/>'
                +'</div>'
                +'<div class="list-item reason-btn" style="position: relative;cursor:pointer;">'
                +    '<span class="reason-item" ><span class="reasonbox" data-id="4" style="font-size:12px">拿错商品</span><span class="iconfont icon-xiala"></span></span>'
                +'</div>'
                +'<div class="list-item" style="padding-top: 25px">'
                +    '<div class="check" style="margin:0 auto">'
                +        '<input type="checkbox" name="destroy">'
                +       ' <label></label>'
                +    '</div>'
                +'</div>'
                +'</li>'
            $('.change-box ul').append(li);  
            getTotalPrice();
        })
        //商品加减
        
        $('body').on('click','.icon-jian',function(){
        	
        	var index=$(this).parents('.order-table-list').attr('data-index');//商品所在的位置，
        	var num=items[index].exchange.num;
        	var price=items[index].price;
        	var type=items[index].exchange.type;
        	num--;
        	
        	if(num < 0) return;
        	if(num == 0){
        		delete items[index].exchange;
        		$(this).parents('.order-table-list').remove();
        		$('.order-detail-list').eq(index).find('.list-select').addClass('btn-primary').removeClass('btn-default');
        	}
        	if(num>0){
        		if( type == 0){
	        		items[index].exchange.money=parseFloat(num*price*discount).toFixed(2);
	        		$(this).parents('.order-table-list').find('.refund-input').val(items[index].exchange.money);
	        		console.log(items[index].exchange.money)
	        	}
	        	items[index].exchange.num = num;
	        	$(this).parents('.order-table-list').find('.menu-list-num').val(num);
        	}
        	
        	getTotalPrice()
        })
        $('body').on('click','.icon-add',function(){
        	var index=$(this).parents('.order-table-list').attr('data-index');//商品所在的位置，
        	var num=items[index].exchange.num;
        	var price=items[index].price;
        	var type=items[index].exchange.type;
        	
        	num++;
        	console.log(num,items[index].qty,items)
        	if(parseInt(num) > parseInt(items[index].qty)) return;
        	if(type == 0){
        		items[index].exchange.money=parseFloat(num*price*discount).toFixed(2);
        		$(this).parents('.order-table-list').find('.refund-input').val(items[index].exchange.money);
        	}
        	items[index].exchange.num = num;
        	$(this).parents('.order-table-list').find('.menu-list-num').val(num);
        	getTotalPrice()
        })
            
//        退换货切换
        $('body').on('click','.switch-tab span',function () {
        	console.log(discount)
        	var index=$(this).parents('.order-table-list').attr('data-index');
        	var num=items[index].exchange.num;
        	var price=items[index].price;
        	var type;
            if($(this).hasClass('active')){
                type=$(this).siblings().attr('data-type');
               
              $(this).removeClass('active').siblings().addClass('active');
            }else{
                $(this).addClass('active').siblings().removeClass('active');
               type=$(this).attr('data-type');
               
            }
            console.log(type)
            items[index].exchange.type=type;
            
            if(type == 1){
            	items[index].exchange.money = 0
            	
            	$(this).parents('.order-table-list').find('.refund-input').val(items[index].exchange.money);
            }else{
            	items[index].exchange.money=parseFloat(num*price*discount).toFixed(2);
            	
            	$(this).parents('.order-table-list').find('.refund-input').val(items[index].exchange.money);
            }
            getTotalPrice()
        });
        //手动改变商品数量
         $('body').on('focusout','.menu-list-num',function () {
         	var index=$(this).parents('.order-table-list').attr('data-index');
         	var num=$(this).val();
        	var price=items[index].price;
        	var type=items[index].exchange.type;
        	console.log(num)
        	if(num < 0 || num > items[index].qty) {
        		$(this).val(items[index].qty);
        		if(type == 1){
	            	items[index].exchange.money = 0
	            	$(this).parents('.order-table-list').find('.refund-input').val(0);
	            }else{
	            	cart[index].money=parseFloat(items[index].qty*price*discount).toFixed(2);
	            	$(this).parents('.order-table-list').find('.refund-input').val(cart[index].money);
	            }
        	}else{
        		$(this).val(num);
        		items[index].exchange.num=num;
        		if(type == 1){
	            	items[index].exchange.money = 0
	            	$(this).parents('.order-table-list').find('.refund-input').val(0);
	            }else{
	            	cart[index].money=parseFloat(num*price*discount).toFixed(2);
	            	$(this).parents('.order-table-list').find('.refund-input').val(cart[index].money);
	            }
        	};
        	getTotalPrice()
         })
         //手动改变退款金额
         $('body').on('focusout','.refund-input',function () {
         	var index=$(this).parents('.order-table-list').attr('data-index');
         	var money=$(this).val();
        	var type=items[index].exchange.type;
        	if(type == 1){
        		$(this).val(0);
        	}else{
        		items[index].exchange.money = money;
        	}
        	getTotalPrice()
         })
        
        //弹窗
        $('.returnButton').on('click',function () {
            pop();
            $('#tuikuan').show().siblings().hide();
            $('.discount-val').val((totalPrice).toFixed(2));
            $('.oldPrice').html(total);
            $('.discount').html(discount);
            $('.newPrice').html((total-cut_fee).toFixed(2));
            if(user_id>0){
            	$('.refund-cash').hide();
            	pay_mode=0;
            }else{
            	$('.refund-balance').hide();
            	pay_mode =1;
            }
        });
        //页面取消
        $('.cancelButton').click(function(){
        	clearRefund();
        	$('.change-detail').hide();
        	$('.change-search').show();
        })
 
         //提交
         var pay_mode;
         $('.refund-submit').click(function(){
         	var order_item_id=[];
         	var product_id =[];
         	var item_id=[];
         	var qty=[];
         	var money=[];
         	var type=[];
         	var is_damaged=[];
         	var reason=[];
         	$('.change-box ul li').each(function(){
         		reason.push($(this).find('.reasonbox').html());
         		is_damaged.push( Number($(this).find('input[type="checkbox"]').is(':checked')) )
         	})
         	$.each(items,function(index,item){
         		if(item.exchange){
         			order_item_id.push(item.id);
         			product_id.push(item.product_id);
         			item_id.push(item.item_id);
         			qty.push(item.exchange.num);
         			money.push(item.exchange.money);
         			type.push(item.exchange.type);
         		}
         	})
         	var data={
         		order_id:order_id,
         		pay_mode:pay_mode,
         		remark:$('.note-val').val() || null,
         		order_item_id:order_item_id,
         		product_id:product_id,
         		item_id:item_id,
         		qty:qty,
         		money:money,
         		type:type,
         		reason:reason,
         		is_damaged:is_damaged
         	}
         	console.log(data);
         	$.ajax({
         		type:'POST',
         		data:data,
         		dataType:'json',
         		url:urlHead+'/order/refund',
         		success:function(json){
         			refundPrint(json)
         			tip(lang('CHANGE_ORDER_SUCCESS'));
         			clearRefund();
         			$('.change-search').show();
         			$('.change-detail').hide();
         			$('.popbg').hide();
         		}
         	})
         })
		//退货原因
		var reasonlist='',reasonId=0;
		var ii;//退货原因所在位置
        $('body').on('click','.reason-btn',function () {
        	
        	ii=$(this).parents('.order-table-list').index();
            pop();
            $('#reason').show().siblings().hide(); 
            reasonlist=$(this).find('.reasonbox').html();
        	reasonId=$(this).find('.reasonbox').attr('data-id');
            $('.reason-val').val(reasonlist);
            $('.reason-list').eq(reasonId).addClass('active').siblings().removeClass('active');
              
        });
        $('#reason').on('click','.reason-list',function(){
        	reasonId=$(this).attr('data-id');
        	$(this).addClass('active').siblings().removeClass('active');
        	$('.reason-val').val($(this).html());
        	  
        })
        $('.reason-submit-btn').click(function(){
       		reasonlist=$('.reason-val').val()
        	$('.change-box ul li').eq(ii).find('.reasonbox').html(reasonlist);
        	$('.change-box ul li').eq(ii).find('.reasonbox').attr('data-id',reasonId);
        	console.log(reasonId);
        	if(reasonId == 0 || reasonId == 1){
        		$('.change-box ul li').eq(ii).find('.check input').prop('checked',true);
        	}else{
        		$('.change-box ul li').eq(ii).find('.check input').prop('checked',false);
        	}
        	$('.popbg').hide();
        	$('.reason-val').val('');
        })
        //总价
       var totalPrice;
       function getTotalPrice(){
       		totalPrice=0;
//     		var cut_item_fee=0;
//     		if(cut_fee>0){
//     			cut_item_fee=parseFloat(cut_fee)/parseFloat(items.length)
//     		}
       		
       		if($('.list-item .refund-input').length>0){
       			$('.list-item .refund-input').each(function(){
//	       			totalPrice+=parseFloat($(this).val())-parseFloat(cut_item_fee);
					totalPrice+=parseFloat($(this).val());
	       		})
       		}
       		$('.return').html((totalPrice).toFixed(2));
       }
       //清空
       function clearRefund(){
       		$('.change-box ul li').remove();
        	$('.order-detail-list').each(function(index,item){
        		$(this).find('.list-select').addClass('btn-primary').removeClass('btn-default');
        	})
        	$.each(items,function(index,item){
        		if(item.exchange){
        			delete item.exchange;
        		}
        	});
        	$('.return').html(0);
        	totalPrice=0
       }
       function refundPrint(data){
	       	$('#printer').show().siblings().hide();
	       	var paymode = SITE_CONFIG.pay_types[data.order_refund.pay_mode][SITE_LANG]; 
	       	if(user_id>0) paymode="会员卡"
	        
			$('.print-refund .cashier-name').html(CASHIER);
			$('.print-refund .refund-time').html(data.order_refund.create_time);
			$('.print-refund .refund-id').html(refundOrder.order_id);
			$('.print-refund .print-refund-payMode').html(paymode+'退款');
			var food=''
			var count=0;
			var damageds=0;
			$.each(data.refunds,function(index,list){
				count+=Number(list.qty);
				if(list.is_damaged == 1){
					damageds+=Number(list.qty);
				}
				
				food+='<div class="print-table-list">'
					+'<div class="print-food">'+(index+1)+'.'+list.title+'</div>'
					+'<div class="print-food-detail">'
					+	'<span>'+list.price+'</span>'
					+	'<span>'+list.qty+'</span>'
					+	'<span>'+(list.is_damaged==0?'否':'是')+'</span>'
					+	'<span>'+list.money+'</span>'
					+'</div>'
				+'</div>'
			
			})
			$('.print-refund .print-table-body').html(food);
			$('.print-refund .print-table-total-food').html(count.toFixed(2));
			$('.print-refund .print-table-total-price').html(data.order_refund.money);
			$('.print-refund .refund-remark').html(data.refunds[0].remark);
			if(user){
				$('.print-refund .vip-name').html(user.username);
				$('.print-refund .vip-weixin').html(user.wechat);
				$('.print-refund .vip-address').html((user.address?user.address:''));
				$('.print-refund .vip-zip').html(user.zip);
				$('.print-refund .vip-phone').html(user.phone);
			}else{
				$('.print-refund .vip-name-box').html('联系姓名：');
			}
			var consumption='';
			consumption+='<div class="print-list" >退款金额：'+data.order_refund.money+'</div>'
			+'<div class="print-list" >'+paymode+'退款：'+data.order_refund.money+'</div>'		
			if(data.order_refund.pay_mode == 0){
				consumption+='<div class="print-list" >上次余额：'+data.order_refund.last+'</div>'
				consumption+='<div class="print-list" >当前余额：'+data.order_refund.now+'</div>'
			}
			consumption+='<div class="print-list" >货损数量：'+damageds+'</div>'
			$('.consumption').html(consumption);
			var vatNet=Number(data.order_refund.money)-Number(data.order_refund.vat)
			$('.print-refund .vat-net').html(vatNet.toFixed(2));
			$('.print-refund .vat-20').html(data.order_refund.vat);
			$('.print-refund .vat-gross').html(data.order_refund.money);
			$('.print-time-detail').html(getTime())
			$('#printer').jqprint();
       }
    })
</script>
</body>
</html>